home *** CD-ROM | disk | FTP | other *** search
Java Source | 2000-07-28 | 5.9 KB | 238 lines |
- /*
- * LevelSelector.java - A selector for levels databases
- * eCross is Copyright (C) 2000 Romain Guy
- * guy.romain@bigfoot.com
- * www.jext.org
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
- import waba.fx.*;
- import waba.io.*;
- import waba.ui.*;
-
- /**
- * This is a level selector.
- * @version 1.0
- * @author Romain Guy <guy.romain@bigfoot.com>
- */
-
- public class LevelSelector extends Bounded
- {
- private byte state = 0;
- private boolean lGray, rGray;
- private int currentLevel = 0, maxLevels;
-
- private Catalog levels;
- private String dataBase, info;
-
- // arrows
- private Image leftArrow = new Image("datas/larrow.bmp");
- private Image leftGrayedArrow = new Image("datas/glarrow.bmp");
- private Image rightArrow = new Image("datas/rarrow.bmp");
- private Image rightGrayedArrow = new Image("datas/grarrow.bmp");
-
- /**
- * Creates a new level selector.
- * @param dataBase Defines the levels database
- */
-
- public LevelSelector(String dataBase)
- {
- this.dataBase = dataBase;
- }
-
- /**
- * Handles a pen down event.
- * @param parent The caller
- * @param g The <code>Graphics</code> surface of the caller
- * @param x The x coordinate of the event
- * @param y The y coordinate of the event
- * @return <code>true</code> if the event was handled
- */
-
- public boolean handlePenEvent(eCross parent, Graphics g, int x, int y)
- {
- // levels switching
- if (state == 1)
- {
- if (x > 13 && x < 43 && y > 97 && y < 112)
- {
-
- state = 0;
- levels.close();
- parent.setLevel(currentLevel);
- parent.levelFinished();
- //parent.resumeTimer();
-
- } else if (x > 46 && x < 81 && y > 97 && y < 112) {
-
- state = 0;
- levels.close();
- parent.draw();
- parent.resumeTimer();
-
- } else if (!lGray && x > 128 && x < 134 && y > 100 && y < 111) {
-
- if (--currentLevel == 0)
- {
- g.drawImage(leftGrayedArrow, 128, 125);
- lGray = true;
- }
-
- if (rGray)
- {
- g.drawImage(rightArrow, 140, 125);
- rGray = false;
- }
-
- paintLevel(g);
-
- } else if (!rGray && x > 140 && x < 146 && y > 100 && y < 111) {
-
- if (++currentLevel == maxLevels - 1)
- {
- g.drawImage(rightGrayedArrow, 140, 100);
- rGray = true;
- }
-
- if (lGray)
- {
- g.drawImage(leftArrow, 128, 100);
- lGray = false;
- }
-
- paintLevel(g);
- }
-
- return true;
- }
-
- if (x > this.x && x < this.x + this.width &&
- y > this.y && y < this.y + this.height)
- {
- showSelector(parent, g);
- return true;
- }
-
- return false;
- }
-
- /**
- * Paints the level selector.
- */
-
- private void showSelector(eCross parent, Graphics g)
- {
- parent.pauseTimer();
-
- levels = new Catalog(dataBase, Catalog.READ_ONLY);
- if (!levels.isOpen())
- return;
- maxLevels = levels.getRecordCount();
-
- state = 1;
- currentLevel = parent.getLevel();
- lGray = (currentLevel == 0);
- rGray = (currentLevel == maxLevels - 1);
-
- g.setColor(255, 255, 255);
- // erase place of the window
- g.fillRect(9, 54, 142, 62);
-
- g.setColor(0, 0, 0);
- g.drawRect(10, 55, 140, 60);
-
- Font font = new Font("Helvetica", Font.BOLD, 10);
- FontMetrics fm = parent.getFontMetrics(font);
- g.setFont(font);
-
- g.drawRect(13, 97, 30, 15);
- g.drawDots(15, 112, 43, 112);
- g.drawDots(43, 112, 43, 99);
- String text = new String("Ok");
- g.drawText(text, 13 + (30 - fm.getTextWidth(text)) / 2, 99);
-
- g.drawRect(46, 97, 35, 15);
- g.drawDots(48, 112, 81, 112);
- g.drawDots(81, 112, 81, 99);
- text = new String("Cancel");
- g.drawText(text, 46 + 1 + (35 - fm.getTextWidth(text)) / 2, 99);
-
- g.drawImage(lGray ? leftGrayedArrow : leftArrow, 128, 100);
- g.drawImage(rGray ? rightGrayedArrow : rightArrow, 140, 100);
-
- g.setFont(new Font("Helvetica", Font.PLAIN, 10));
- paintLevel(g);
- }
-
- // paints infos about current level
-
- public void paintLevel(Graphics g)
- {
- if (!levels.setRecordPos(currentLevel))
- return;
-
- g.setColor(255, 255, 255);
- g.fillRect(11, 56, 138, 40);
-
- g.setColor(0, 0, 0);
- g.drawText(new StringBuffer("Level ").append(currentLevel + 1).toString(),
- 13, 67);
-
- byte[] b = new byte[2];
- levels.readBytes(b, 0, b.length);
-
- StringBuffer buf = new StringBuffer("Best time: ");
- if (b[0] < 10)
- buf.append('0');
- buf.append(b[0]).append(':');
-
- if (b[1] < 10)
- buf.append('0');
-
- g.drawText(buf.append(b[1]).toString(), 13, 67 + 11);
-
- g.drawText((currentLevel + 1) + "/" + maxLevels, 95, 99);
- }
-
- public void setInfo(String info)
- {
- this.info = info;
- }
-
- public void paint(Control c, Graphics g)
- {
- Font font = new Font("Helvetica", Font.PLAIN, 10);
- g.setFont(font);
- FontMetrics fm = c.getFontMetrics(font);
-
- g.setColor(0, 0, 0);
- g.drawText(info,
- x + 1 + (this.width - fm.getTextWidth(info)) / 2,
- 2);
- }
-
- public void free()
- {
- leftArrow.free();
- leftGrayedArrow.free();
- rightArrow.free();
- rightGrayedArrow.free();
- }
- }
-
- // End of LevelSelector.java
-